<?php
/* This is the theme options page */

//default options
$yui_defaults = array(
	'yui_pages_to_exclude' => ''
);

//option name
$yui_option_name = 'yui_theme_option';

//get options
$yui_options = get_option($yui_option_name);
$yui_options = wp_parse_args($yui_options, $yui_defaults);

//hook to the action
add_action('admin_menu', 'yui_add_theme_page');

function yui_add_theme_page() {
	global $yui_option_name, $yui_options;
	if ( isset( $_GET['page'] ) && $_GET['page'] == basename(__FILE__) ) {
		if ( isset( $_REQUEST['action'] ) && 'save' == $_REQUEST['action'] ) {
			foreach( $yui_options as $key => $value)
			{
				$yui_options[$key] = $_REQUEST[$key];
			}
			update_option($yui_option_name, $yui_options);
		}
		add_action('admin_head','yui_theme_page_head');
	}
	add_theme_page(__('Yui Options'), __('Yui Options'), 'edit_themes', basename(__FILE__), 'yui_theme_page');
}

function yui_theme_page() {
	global $yui_options;
?>
<div class="wrap">
	<h2><?php _e('Yui Options','yui-theme');?></h2>
	<form method="post">
		<table class="form-table">
			<tbody>
				<tr valign="top">
					<th scope="row"><?php _e('Menu Tabs to Show','yui-theme');?></th>
					<td>
						<p><?php _e('Click the page name which you DONNOT want to show as a menu tab.','yui-theme');?></p>
						<ul id="pageslist">
<?php 
	$para_args = array(
		'numberposts' 		=> -1,
		'orderby'			=> 'menu_order',
		'order'				=> 'ASC',
		'post_type'			=> 'page',
	);
	$pages = get_posts($para_args);
	$excludepages = explode(',',$yui_options['yui_pages_to_exclude']);
	if($pages){
		foreach($pages as $page){
			echo '<li id="' . $page->ID . '"';
			if (in_array($page->ID, $excludepages)) echo ' class="itemchecked" name="checked"';
			echo '>' . $page->post_title . '</li>';
		}
	} else {
		_e('You have no pages currently!','yui-theme');
	}
?>
						</ul>
						<p>
							<label for="yui_pages_to_exclude"><?php _e('Pages to EXCLUDE: ','yui-theme');?></label>
							<input id="yui_pages_to_exclude" name="yui_pages_to_exclude" value="<?php echo $yui_options['yui_pages_to_exclude'];?>" type="text" size="30" />
						</p>
					</td>
				</tr>
			</tbody>
		</table>
		<p class="submit">
			<input type="submit" value="<?php _e('Save changes','yui-theme'); ?>" name="save"/>
			<input type="hidden" value="save" name="action"/>
		</p>
	</form>
	<form method="post">
		<p class="submit">
			<input type="submit" value="<?php _e('Reset','yui-theme');?>" name="reset"/>
			<input type="hidden" value="reset" name="action"/>
		</p>
	</form>
</div>
<?php 
}

function yui_theme_page_head() {
?>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
	jQuery('#pageslist').addClass('itemclickable');

	jQuery('#pageslist li').each(function (){
		jQuery(this).click(function(){
			$inputbox = jQuery('#yui_pages_to_exclude');
			$thisli = jQuery(this);
			if ($thisli.attr('name') == "checked")
			{
				var newvalue = $inputbox.val().replace($thisli.attr('id')+',','');
				$thisli.removeClass('itemchecked');
				$thisli.attr('name',"unchecked");
				$inputbox.val(newvalue);
			}
			else
			{
				$thisli.addClass('itemchecked');
				var newvalue = $inputbox.val() + $thisli.attr('id') + ',';
				$thisli.attr('name',"checked");
				$inputbox.attr('value',newvalue);
			}
		});
	});
});
// ]]>
</script>
<style type="text/css">
	#pageslist{
		width:300px;
	}
	#pageslist li{
		line-height:150%;
		margin-bottom:8px;
	}
	.itemclickable li:hover{
		color:#00e;
		cursor:pointer;
	}
	.itemchecked {
		background-color:#d54e21;
		color:#ddd;
		text-decoration:line-through;
	}
</style>
<?php
}
?>